//
//  BCMoreViewController.m
//  BCManagerVersion
//
//  Created by 吴 崧 on 14-3-5.
//  Copyright (c) 2014年 Jungle. All rights reserved.
//

#import "BCMoreViewController.h"
#import "BCProgressHUD.h"
#import "BCAdItemViewController.h"

#define Push_tag 1
#define New_tag 2
#define Sound_tag 3
#define Vibrator_tag 4


@interface BCMoreViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UISwitch *canPushSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *canRecieveNewSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *canSoundSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *canVibratorSwitch;

@property (nonatomic, strong) UIPageViewController *bannerPageViewController;
@property (nonatomic, strong) NSMutableArray *banners;
@property (nonatomic, strong) NSMutableArray *bannerViewControllers;
@end

@implementation BCMoreViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self setSwithStatus];
    
    self.banners = [NSMutableArray array];
	self.bannerViewControllers = [NSMutableArray array];
    [BCProgressHUD showWithTile:@"下载中" animated:YES];
    [BCNetworkClient sysads:0
                      limit:0
                    success:^(NSNumber *total, NSString *type, NSArray *ads) {
                        [BCProgressHUD hideAnimated:YES];
                        
                        if (total.intValue > 0) {
                            self.bannerPageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
                            
                            
                            self.bannerPageViewController.delegate = self;
                            self.bannerPageViewController.dataSource = self;
                            
                            self.bannerPageViewController.view.frame = self.bannerView.bounds;
                            
                            [self.bannerView addSubview:self.bannerPageViewController.view];
                        }
                        [self.banners addObjectsFromArray:ads];
                        
                        [ads enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
                            UIViewController *vc = [[BCAdItemViewController alloc] initWithTitle:obj[@"title"] message:obj[@"message"] imageUrl:obj[@"image"] size:self.bannerView.frame.size];
                            
                            [self.bannerViewControllers addObject:vc];
                        }];
                        
                        if (self.bannerViewControllers.count > 0) {
                            [self.bannerPageViewController setViewControllers:@[self.bannerViewControllers[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion: ^(BOOL finished) {
                            }];
                        }
                        
                        self.bannerPageControl.numberOfPages = self.bannerViewControllers.count;
                        
                    } failure:^(NSError *error) {
                        [BCProgressHUD hideWithError:error animated:YES];
                    }];
    
}





-(void)setSwithStatus
{
    self.canPushSwitch.on = USER_MANAGER.canPush;
    self.canRecieveNewSwitch.on = USER_MANAGER.canAlert;
    self.canSoundSwitch.on = USER_MANAGER.canSound;
    self.canVibratorSwitch.on = USER_MANAGER.canVibrator;
}






- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)about:(UIButton *)sender {
    [BCProgressHUD showWithTile:@"加载中" animated:YES];
    [BCNetworkClient aboutUsSuccess:^(NSString *message) {
        [BCProgressHUD hideAnimated:YES];
        ExhibitionView *ex = [[ExhibitionView alloc]initWithString:message];
        [ex Show];
    } failure:^(NSError *error) {
        [BCProgressHUD hideWithError:error animated:YES];
    }];
}


- (IBAction)logOut:(id)sender {
    USER_MANAGER.auth = nil;
    [self.tabBarController setSelectedViewController:[self.tabBarController.viewControllers firstObject]];
}



- (IBAction)switchChanged:(UISwitch *)sender {
    switch (sender.tag) {
        case Push_tag:
            USER_MANAGER.canPush = sender.on;
            break;
        case New_tag:
            USER_MANAGER.canAlert = sender.on;
            break;
        case Sound_tag:
            USER_MANAGER.canSound = sender.on;
            break;
        default:
            USER_MANAGER.canVibrator = sender.on;
            break;
    }
    [USER_MANAGER save];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (nil == viewController) {
        return self.bannerViewControllers.firstObject;
    }
    NSInteger idx = [self.bannerViewControllers indexOfObject:viewController];
    NSParameterAssert(idx != NSNotFound);
    if (idx >= [self.bannerViewControllers count] - 1) {
        // we're at the end of the _pages array
        return self.bannerViewControllers[0];
    }
    // return the next page's view controller
    return self.bannerViewControllers[idx + 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if (nil == viewController) {
        return self.bannerViewControllers.firstObject;
    }
    NSInteger idx = [self.bannerViewControllers indexOfObject:viewController];
    NSParameterAssert(idx != NSNotFound);
    if (idx <= 0) {
        // we're at the end of the _pages array
        return self.bannerViewControllers.lastObject;
    }
    // return the previous page's view controller
    return self.bannerViewControllers[idx - 1];
}

- (void)pageViewController:(UIPageViewController *)viewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (!completed) {
        return;
    }
    
    // Find index of current page
    
    NSUInteger indexOfCurrentPage = [self.bannerViewControllers indexOfObject:[viewController.viewControllers lastObject]];
    
    self.bannerPageControl.currentPage = indexOfCurrentPage;
}
@end
